<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.status" placeholder="状态" class="filter-item" @change="handleFilter">
        <el-option label="全部" value="" />
        <el-option label="通过" value="1" />
        <el-option label="未通过" value="2" />
      </el-select>
      <el-select v-model="listQuery.firm_id" placeholder="家族长" class="filter-item" @change="handleFilter">
        <el-option label="全部" value="" />
        <el-option v-for="firm in firmAll" :label="firm.name" :value="firm.id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="昵称" min-width="120" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.nickname }}</a>
        </template>
      </el-table-column>
      <el-table-column label="微信" min-width="120" align="center">
        <template slot-scope="{row}">
          <a href="javascript:;">{{ row.application_wechat }}</a>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center">
        <template slot-scope="{row}">
          <el-image :src="row.head_img" :preview-src-list="[row.head_img]" class="dialog_img" />
        </template>
      </el-table-column>
      <el-table-column label="聊天收益" min-width="100" align="center">
        <template slot-scope="{row}">
          <a href="javascript:;">{{ row.chat_amount }}</a>
        </template>
      </el-table-column>
      <el-table-column label="礼物收益" min-width="100" align="center">
        <template slot-scope="{row}">
          <a href="javascript:;">{{ row.gift_rate }} %</a>
        </template>
      </el-table-column>
      <el-table-column label="语聊收益" min-width="100" align="center">
        <template slot-scope="{row}">
          <a href="javascript:;">{{ row.voice_amount }}</a>
        </template>
      </el-table-column>
      <el-table-column label="是否家族" align="center">
        <template slot-scope="{row}">
          <span>{{ row.firm_id ? '是' : '无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <a v-if="row.status === 0" class="click_a" @click="agreeAnchor(row.id)">
            通过
          </a>
          <a v-if="row.status === 0" class="click_a" @click="handleRefuse(row.id)">
            拒绝
          </a>
          <a class="click_a" @click="handleUpdate(row)">
            编辑
          </a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <div class="components-container">
      <el-dialog v-el-drag-dialog :visible.sync="dialogVisibleUserInfo" title="用户详情" width="35%" top="0">
        <el-form label-width="80px" label-position="left">
          <el-form-item label="昵称">
            <span>{{ userInfo.nickname }}</span>
          </el-form-item>
          <el-form-item label="简介">
            <span>{{ userInfo.nickname }}</span>
          </el-form-item>
          <el-form-item label="微信">
            <span>{{ userInfo.wechat ? userInfo.wechat : '未绑定' }}</span>
          </el-form-item>
          <el-form-item label="真人">
            <span>{{ userInfo.is_real ? '已认证' : '未认证' }}</span>
          </el-form-item>
          <el-form-item label="主播">
            <span>{{ userInfo.is_anchor ? '是' : '否' }}</span>
          </el-form-item>
          <el-form-item label="在线">
            <span>{{ userInfo.is_online ? '在线' : '离线' }}</span>
          </el-form-item>
          <el-form-item label="性别">
            <span>{{ userInfo.sex | formatSex }}</span>
          </el-form-item>
          <el-form-item label="生日">
            <span>{{ userInfo.birthday }}</span>
          </el-form-item>
          <el-form-item label="城市">
            <span>{{ userInfo.city }}</span>
          </el-form-item>
          <el-form-item label="注册时间">
            <span>{{ userInfo.created_at | formatTime }}</span>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleCoins"
        size="1200px"
        title="金币日志"
      />
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleMoney"
        size="1200px"
        title="余额日志"
      />
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="margin: 0 45px">
        <el-form-item label="状态">
          <el-radio-group v-model="temp.status">
            <el-radio v-for="status in statusOptions" :label="status.key" border>
              {{ status.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="申请微信" prop="chat_amount">
          <el-input v-model="temp.application_wechat" />
        </el-form-item>
        <el-form-item label="聊天收益（金额）" prop="chat_amount">
          <el-input v-model="temp.chat_amount" />
        </el-form-item>
        <el-form-item label="礼物收益（百分比）" prop="gift_rate">
          <el-input v-model="temp.gift_rate" />
        </el-form-item>
        <el-form-item label="语音收益（金额）" prop="voice_amount">
          <el-input v-model="temp.voice_amount" />
        </el-form-item>
        <el-form-item label="家族长" prop="firm_id">
          <el-select v-model="temp.firm_id" placeholder="状态" class="filter-item">
            <el-option label="无" value="0" />
            <el-option v-for="firm in firmAll" :label="firm.name" :value="firm.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>

    <el-dialog v-el-drag-dialog :visible.sync="dialogVisibleRefuse" title="拒绝理由" width="35%">
      <el-form label-width="80px" label-position="left">
        <el-form-item label="理由">
          <el-select v-model="refuse" placeholder="拒绝理由" class="filter-item" @change="handleFilter">
            <el-option v-for="refuse in refuses" :label="refuse" :value="refuse" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleRefuse = false">
          取消
        </el-button>
        <el-button type="primary" @click="refuseAnchor">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { anchorList, auditAnchor, updateAnchor } from '@/api/anchor'
import { allFirm } from '@/api/firm'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    statusText(status) {
      const statusMap = {
        1: '通过',
        2: '未通过'
      }
      return statusMap[parseInt(status)]
    },
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    },
    formatSex: function(sex) {
      const sexMap = {
        0: '未知',
        1: '男',
        2: '女'
      }
      return sexMap[parseInt(sex)]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        sort: 'desc',
        sort_field: 'id',
        status: '',
        firm_id: ''
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'change' }],
        chat_amount: [{ required: true, message: '请输入聊天收益', trigger: 'change' }],
        gift_rate: [{ required: true, message: '请输入礼物收益', trigger: 'change' }],
        voice_amount: [{ required: true, message: '请输入语聊收益', trigger: 'change' }]
      },
      downloadLoading: false,
      dialogVisibleUserInfo: false,
      drawerVisibleOrder: false,
      drawerVisibleCoins: false,
      drawerVisibleMoney: false,
      userInfo: {},
      dialogFormVisible: false,
      firmAll: [],
      statusOptions: [{ label: '待审核', key: 0 }, { label: '通过', key: 1 }, { label: '未通过', key: 2 }],
      temp: {
        id: undefined,
        name: '',
        username: '',
        password: '',
        status: 1,
        chat_amount: '',
        gift_rate: '',
        voice_amount: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogVisibleRefuse: false,
      refuses: [
        '图片涉黄。',
        '图片带有联系方式，请保护好您的敏感信息。',
        '图片侵权或存在水印。',
        '图片存在敏感人物。',
        '图片存在暴力、恐怖血腥、反动内容。',
        '图片存在二维码或者广告信息。',
        '图片存在易引发他人不适。'
      ],
      refuse: ''
    }
  },
  created() {
    this.getList()
    this.getFirm()
  },
  methods: {
    getList() {
      this.listLoading = true
      anchorList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    getFirm() {
      allFirm().then(response => {
        this.firmAll = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    },
    getUserInfo: function(item) {
      this.userInfo = item
      this.dialogVisibleUserInfo = true
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        username: '',
        password: '',
        status: 1,
        chat_amount: '',
        gift_rate: '',
        voice_amount: ''
      }
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateAnchor(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '成功编辑主播',
              type: 'success',
              duration: 1500
            })
            this.getList()
          })
        }
      })
    },
    agreeAnchor(id) {
      auditAnchor({ id: id, status: 1 }).then((response) => {
        if (response.code === 1) {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
        }
      })
    },
    handleRefuse(id) {
      this.refuseId = id
      this.dialogVisibleRefuse = true
    },
    refuseAnchor() {
      const data = {
        id: this.refuseId,
        status: 2,
        refuse_info: this.refuse
      }
      auditAnchor(data).then((response) => {
        if (response.code === 1) {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.dialogVisibleRefuse = false
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
        }
      })
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
.el-form-item__label{width: 150px !important;}
.el-form-item__content{margin-left: 150px !important;}
</style>

